<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addUser">添加</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table :data="userList" border stripe style="width: 100%">
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="password" label="密码"></el-table-column>
      <el-table-column prop="createTime" :formatter="dateFormat" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" :formatter="dateFormat" label="更新时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="修改" placement="top" :enterable="false">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editUser(scope.row.id)"
            ></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteUser(scope.row.id)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
// import dateFormat from "../../utils/dateFormat";
import { getUsers,deleteUserById } from "@/api/data";
import getDataFormat from "@/utils/dateFormat";
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageSize: 5,
        pageNum: 1
      },
      userList: [],
      total: 0,
      pageNum: 1
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      // const { data: res } = await this.$http.get("/users", {
      //   params: {
      //     query: this.queryInfo
      //   }
      // });
      let { data: res } = await getUsers({query: this.queryInfo});
      this.userList = res.data;
      this.total = res.total;
      this.pageNum = res.pageNum;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList();
    },
    addUser() {
      this.$router.push("/user/add");
    },
    editUser(id) {
      this.$router.push({ path: "/user/edit/" + id });
    },
    async deleteUser(id) {
      const confirmRes = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmRes === "confirm") {
        // await this.$http.delete("users/" + id);
        await deleteUserById(id)
        this.$message.success({ message: "删除成功", duration: 1000 });
        this.queryInfo.pageNum = 1;
        this.getUserList();
      } else {
        this.$message.info({ message: "取消删除", duration: 1000 });
      }
    },
    // 格式化时间
    dateFormat(row, column, cellValue, index) {
      return getDataFormat.getDataFormat(row, column, cellValue, index);
    }
  }
};
</script>

<style lang="less" scoped>
</style>